// pages/user/user-balance/index.js
import __config from "../../../config/env";

const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: app.globalData.CustomBar,
    theme: app.globalData.theme, //全局颜色变量
    form: {
      id: '',
      serviceCity: '',
      providerName: '',
      idCard: '',
      idCardImg: '',
      idCardImg2: '',
      startDate: '2023-01-01'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let serviceCity = options.serviceCity ? decodeURI(options.serviceCity) : ''
    this.setData({
      "form.serviceCity": serviceCity
    })
  },
  providerNameInputValue(e) {
    this.setData({
      "form.providerName": e.detail.value
    })
  },
  idcardInputValue(e) {
    this.setData({
      "form.idCard": e.detail.value
    })
  },
  DateChange(e) {
    this.setData({
      "form.startDate": e.detail.value
    })
  },
  ChooseImage(e) {
    const that = this
    let index = e.currentTarget.dataset.index
    wx.chooseMedia({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], //从相册选择
      mediaType: ['image'],
      success: (res) => {
        wx.showLoading({
          title: '加载中',
        })
        let filePath = res.tempFiles[0].tempFilePath
        // 上传图片到后台
        wx.uploadFile({
          url: __config.basePath + '/mall/api/ma/file/upload',
          filePath: filePath,
          header: {
            'app-id': wx.getAccountInfoSync().miniProgram.appId,
            'third-session': wx.getStorageSync('third_session') != null ? wx.getStorageSync('third_session') : ''
          },
          name: 'file',
          formData: {
            'fileType': 'image',
            'dir': 'provider/idCard/'
          },
          success(uploadFileRes) {
            if (uploadFileRes.statusCode == '200') {
              let link = JSON.parse(uploadFileRes.data).link
              if (index == 1) {
                that.setData({
                  "form.idCardImg": link
                })
              } else if (index == 2) {
                that.setData({
                  "form.idCardImg2": link
                })
              }
            } else {
              wx.showModal({
                title: '提示',
                content: '上传失败：' + uploadFileRes.data,
                success(res) {}
              });
            }
          },
          fail: (err) => {
            console.log(err)
          },
          complete(res) {
            wx.hideLoading()
          }
        })
      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: [e.currentTarget.dataset.url],
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '提示',
      content: '确定删除？',
      cancelText: '取消',
      confirmText: '确认',
      success: res => {
        if (res.confirm) {
          let index = e.currentTarget.dataset.index
          if (index == 1) {
            this.setData({
              "form.idCardImg": ''
            })
          } else if (index == 2) {
            this.setData({
              "form.idCardImg2": ''
            })
          }
        }
      }
    })
  },
  formSubmit: function (e) {
    let form = this.data.form
    let errorMsg = ""
    if (!form.providerName) {
      errorMsg = "请输入师傅姓名"
    } else if (!form.idCard) {
      errorMsg = "请输入身份证号"
    } else if (!form.idCardImg) {
      errorMsg = "请上传身份证人像面"
    } else if (!form.idCardImg2) {
      errorMsg = "请上传身份证国徽面"
    }
    if (errorMsg) {
      wx.showToast({
        title: errorMsg,
        icon: "none",
        duration: 2000
      });
    } else {
      if (form.id) {
        // 更新师傅信息
      } else {
        // 申请师傅
        app.api.providerApply(form).then(res => {
          wx.showToast({
            title: '申请成功，等待平台审核',
            icon: 'success',
            duration: 2000,
            success: function () {
              wx.reLaunch({
                url: '/pages/home/index',
              })
            }
          })
        })
      }
    }
  },
})